package com.yao.controller;

import com.yao.common.Result;
import com.yao.po.*;
import com.yao.service.*;
import com.yao.service.impl.FlowersServiceImpl;
import com.yao.utils.BeanUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

/**
 * @author 获取静态资源
 * 时间:   2024/12/17
 */
@RestController
@RequestMapping("/index")
public class GetAssetsController {
    private final ISliderService sliderService;
    private final IProductnavigationService productnavigationService;
    private final IActivityService activityService;
    private final FlowersServiceImpl flowersService;
    private final IPopularService popularService;
    private final IFlowersImagesService flowersImagesService;
    /**
     * 服务器轮播图地址
     */
    private static final String PATH = "http://localhost:8080/assets/banner/";
    private static final Logger log = LoggerFactory.getLogger(GetAssetsController.class);

    public GetAssetsController(ISliderService sliderService, IProductnavigationService productnavigationService, IActivityService activityService, FlowersServiceImpl flowersService, IPopularService popularService, IFlowersImagesService flowersImagesService) {
        this.sliderService = sliderService;
        this.productnavigationService = productnavigationService;
        this.activityService = activityService;
        this.flowersService = flowersService;
        this.popularService = popularService;
        this.flowersImagesService = flowersImagesService;
    }

    /**
     * 获取轮播图资源
     *
     * @return 数组
     */
    @GetMapping("/findBanner")
    public Result<Object> getAssets() {
        log.info("获取轮播图资源...");
        List<Slider> list1 = sliderService.list();
        if (CollectionUtils.isEmpty(list1)) {
            return Result.error("没有找到轮播图资源");
        }
        Object[] array = list1.toArray();
        return Result.success(array);
    }

    /**
     * 获取商品导航资源
     *
     * @return 数组
     */
    @GetMapping("/findCategory1")
    public Result<Object> getNavigation() {
        log.info("获取商品导航资源...");
        List<Productnavigation> list = productnavigationService.list();
        Object[] array = list.toArray();

        if (array.length == 0) {
            return Result.error("没有商品导航资源");
        }
        return Result.success(array);
    }

    /**
     * 获取活动商品资源
     *
     * @return 数组
     */
    @GetMapping("/advertisement")
    public Result<Object> getActivities() {
        log.info("获取活动商品资源...");
        List<Activity> list = activityService.list();
        if (list.isEmpty()) {
            return Result.error("没有活动商品资源");
        }
        return Result.success(list);
    }

    /**
     * 获取猜你喜欢商品资源
     *
     * @return 数组
     */
    @GetMapping("/findListGoods")
    public Result<Object> getLikeGoods() {
        log.info("获取喜欢的商品资源...");
        List<Flowers> list = flowersService.list();

        List<String> lis = new ArrayList<>();

        List<Flower> coolFlowers = BeanUtils.copyList(list, Flower.class);
        int i=0;
        for (Flowers flowers : list) {
            flowersImagesService.lambdaQuery().eq(FlowersImages::getFlowersId, flowers.getFloralLanguage()).list()
                    .forEach(ll -> lis.add(ll.getImageUrl()));
            coolFlowers.get(i++).setFloralLanguage(lis.stream().toList());
            lis.clear();
        }
        if (list.isEmpty()) {
            return Result.error("没有喜欢的商品资源");
        }
        return Result.success(coolFlowers);
    }

    /**
     * 获取热销商品资源
     *
     * @return
     */
    @GetMapping("/findRecommendGoods")
    public Result<Object> getPopularGoods() {
        log.info("获取热销商品资源...");

        List<Popular> list = popularService.list();
        if (CollectionUtils.isEmpty(list)) {
            return Result.error("没有热销商品资源");
        }
        return Result.success(list);
    }

}
